package com.jsy.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import com.jsy.common.util.HttpClient;
import com.jsy.search.service.WeixinPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Value("${weixin.appid}")
    private String appid;
    @Value("${weixin.partner}")
    private String partner;
    @Value("${weixin.partnerkey}")
    private String partnerkey;
    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /**
     * 创建二维码链接步骤
     * 1.封装请求参数数据 2.放松请求 3.获取并处理响应结果
     *
     * @param requestParamMap 请求参数 订单号outTradeNo 支付金额money
     * @return 操作结果
     */
    @Override
    public Map<String, String> createNative(Map<String, String> requestParamMap) {
        try {
            //1.封装数据
            HashMap<String, String> returnMap = new HashMap<>();
            HashMap<String, String> paramMap = new HashMap<>();

            paramMap.put("appid", appid);
            paramMap.put("mch_id", partner);
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("body", "金水永商城");
            paramMap.put("out_trade_no", requestParamMap.get("outTradeNo"));
            paramMap.put("total_fee", requestParamMap.get("money"));
            paramMap.put("spbill_create_ip", "127.0.0.1");
            paramMap.put("notify_url", notifyurl);
            paramMap.put("trade_type", "NATIVE");
            //添加秒杀额外信息attach 用户名 时间 订单类型
            HashMap<String, String> attachMap = new HashMap<>();
            String type = "order";
            if (requestParamMap.containsKey("type") && "seckill".equals(requestParamMap.get("type"))) {
                type = requestParamMap.get("type");
            }
            attachMap.put("type", type);
            if (requestParamMap.containsKey("username")) {
                attachMap.put("username", requestParamMap.get("username"));
            }
            if (requestParamMap.containsKey("time")) {
                attachMap.put("time", requestParamMap.get("time"));
            }
            paramMap.put("attach", JSON.toJSONString(attachMap));
            //转换为带签名的xml
            String signedXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(signedXml);
            httpClient.post();
            //3.处理返回数据
            String content = httpClient.getContent();
            System.out.println("微信统一下单返回的内容为： " + content);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
            returnMap.put("result_code", resultMap.get("result_code"));
            returnMap.put("code_url", resultMap.get("code_url"));
            returnMap.put("out_trade_no", resultMap.get("outTradeNo"));
            returnMap.put("total_fee", resultMap.get("money"));
            return returnMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map<String, String> queryStatus(String outTradeNo) {
        try {
            HashMap<String, String> paramMap = new HashMap<>();
            paramMap.put("out_trade_no", outTradeNo);
            paramMap.put("appid", appid);
            paramMap.put("mch_id", partner);
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            String signedXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);

            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setXmlParam(signedXml);
            httpClient.setHttps(true);
            httpClient.post();

            String content = httpClient.getContent();
            System.out.println("content = " + content);
            return WXPayUtil.xmlToMap(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据订单关闭微信支付系统的订单
     *
     * @param outTradeNo 订单号
     * @return 操作结果
     */
    @Override
    public Map<String, String> closeOrder(String outTradeNo) {
        try {
            HashMap<String, String> paramMap = new HashMap<>();
            paramMap.put("appid", appid);
            paramMap.put("mch_id", partner);
            paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
            paramMap.put("out_trade_no", outTradeNo);

            String signedXml = WXPayUtil.generateSignedXml(paramMap, partnerkey);
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/closeorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(signedXml);
            httpClient.post();

            String content = httpClient.getContent();
            return WXPayUtil.xmlToMap(content);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
